package com.osfw.framework.config;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.osfw.framework.utils.DateUtils;
import com.osfw.framework.utils.StringUtils;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.context.annotation.Configuration;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.Objects;

@Configuration
public class MybatisPlusFieldFillConfig implements MetaObjectHandler {

    private static final String CREATED_TIME = "createTime";
    private static final String CREATED_USER = "createBy";
    private static final String CREATED_USER_NAME = "createByName";
    private static final String UPDATE_TIME = "updateTime";
    private static final String UPDATE_USER = "updateBy";
    private static final String UPDATE_USER_NAME = "updateByName";
    private static final String IS_DELETED = "delFlag";

    private static final String LOGIN_TIME = "loginTime";

    private static final String OPER_TIME = "operTime";

    @Override
    public void insertFill(MetaObject metaObject) {

        if (metaObject.hasGetter(LOGIN_TIME) && !(metaObject.metaObjectForProperty(LOGIN_TIME).hasGetter("value"))) {
            setFieldValByName(LOGIN_TIME, DateUtils.getNowDate(), metaObject);
        }

        if (metaObject.hasGetter(OPER_TIME) && !(metaObject.metaObjectForProperty(OPER_TIME).hasGetter("value"))) {
            setFieldValByName(OPER_TIME, DateUtils.getNowDate(), metaObject);
        }

        if (metaObject.hasGetter(CREATED_USER) && !(metaObject.metaObjectForProperty(CREATED_USER).hasGetter("value"))) {
            setFieldValByName(CREATED_USER, StpUtil.getLoginIdAsLong(), metaObject);
        }
        if (metaObject.hasGetter(CREATED_USER_NAME) && !(metaObject.metaObjectForProperty(CREATED_USER_NAME).hasGetter("value"))) {
            setFieldValByName(CREATED_USER_NAME, StpUtil.getSession().get("userName"), metaObject);
        }
        if (metaObject.hasGetter(CREATED_TIME) && !(metaObject.metaObjectForProperty(CREATED_TIME).hasGetter("value"))) {
            setFieldValByName(CREATED_TIME, DateUtils.getNowDate(), metaObject);
        }

        if (metaObject.hasGetter(UPDATE_USER) && !(metaObject.metaObjectForProperty(UPDATE_USER).hasGetter("value"))) {
            setFieldValByName(UPDATE_USER, StpUtil.getLoginIdAsLong(), metaObject);
        }
        if (metaObject.hasGetter(UPDATE_USER_NAME) && !(metaObject.metaObjectForProperty(UPDATE_USER_NAME).hasGetter("value"))) {
            setFieldValByName(UPDATE_USER_NAME, StpUtil.getSession().get("userName"), metaObject);
        }
        if (metaObject.hasGetter(UPDATE_TIME) && !(metaObject.metaObjectForProperty(UPDATE_TIME).hasGetter("value"))) {
            setFieldValByName(UPDATE_TIME, DateUtils.getNowDate(), metaObject);
        }

        if (metaObject.hasGetter(IS_DELETED) && !(metaObject.metaObjectForProperty(IS_DELETED).hasGetter("value"))) {
            setFieldValByName(IS_DELETED, "0", metaObject);
        }
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        if (metaObject.hasGetter(UPDATE_USER) && !(metaObject.metaObjectForProperty(UPDATE_USER).hasGetter("value"))) {
            setFieldValByName(UPDATE_USER, StpUtil.getLoginIdAsLong(), metaObject);
        }
        if (metaObject.hasGetter(UPDATE_USER_NAME) && !(metaObject.metaObjectForProperty(UPDATE_USER_NAME).hasGetter("value"))) {
            setFieldValByName(UPDATE_USER_NAME, StpUtil.getSession().get("userName"), metaObject);
        }
        if (metaObject.hasGetter(UPDATE_TIME) && !(metaObject.metaObjectForProperty(UPDATE_TIME).hasGetter("value"))) {
            setFieldValByName(UPDATE_TIME, DateUtils.getNowDate(), metaObject);
        }
    }

}
